﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.IO;

namespace NewsMine.Utilities
{
    public class TempAnalyzer
    {
        public static string RemoveAllSpecialCharacterForIndexing(string inputString)
        {

            if (string.IsNullOrEmpty(inputString))
                return inputString;

            try
            {
                inputString = HttpUtility.HtmlEncode(inputString);


                string specialChars = "«‘’`~!@#$%^&*()_-+={[}]|\\:;>.<,?/\t\n\"'";

                specialChars = specialChars + char.ConvertFromUtf32(int.Parse("200C", System.Globalization.NumberStyles.HexNumber));

                StringBuilder sbTemp = new StringBuilder(inputString);

                foreach (var c in specialChars.ToCharArray())
                {
                    sbTemp.Replace(c, ' ');
                }

                return Regex.Replace(sbTemp.ToString(), @"\s+", " ").Trim();
            }
            catch(Exception ex)
            {
                TempLogger.Log("Error while removing special characters... ", ex);

                return inputString;
            }
        }

        public static string RemoveNumbersFromString(string inputString)
        {
            if (string.IsNullOrEmpty(inputString))
                return inputString;

            var numRemovedChars = inputString.Split(' ').ToList().Where(w => NewsMine.Utilities.Extensions.IsNumeric(w) == false).ToList();

            return String.Join(" ", numRemovedChars);


            //var output = Regex.Replace(inputString, @"\S+[\d*]\S+", " ", RegexOptions.Singleline);

            //output = Regex.Replace(output, @"\s+", " ").Trim();

            //return output;

        }

        public static string RemoveSingleCharacterWordsFromString(string inputString)
        {
            if (string.IsNullOrEmpty(inputString))
                return inputString;

            string output = string.Join(" ", inputString.Split(new[] { ' ', '\t', '\n', '\r' }).Where(word => word.Length>1));

            return output;

        }

        public static string RemoveLengthyWordsFromString(string inputString)
        {
            if (string.IsNullOrEmpty(inputString))
                return inputString;


            //var output = Regex.Replace(inputString, @"\S+[.]{1,1}\S+", " ");
            var output = Regex.Replace(inputString, @"\b\w{40,1000}\b\s?", " ");


            //output = inputString.replaceAll("\\b[\\w']{1,2}\\b", "");
            //passage = passage.replaceAll("\\s{2,}", " ");

            return Regex.Replace(output, @"\s+", " ").Trim();

        }

        public static string RemoveStopWordsFromString(string inputString)
        {
            //StringBuilder input = new StringBuilder(inputString);

            var stopWords = GetStopWords();

            string output = string.Join(" ", inputString.Split(new[] { ' ', '\t', '\n', '\r' }).Where(word => !stopWords.Contains(word)));

            //foreach (string word in GetStopWords())
            //{
            //    input.Replace(word, "");
            //}

            //return input.ToString();

            return output;
        }

        public static string RemoveNoiceWordsFromString(string inputString)
        {
            var noiceWords = GetNoiceWords();
            string output = string.Join(" ", inputString.Split(new[] { ' ', '\t', '\n', '\r' }).Where(word => !noiceWords.Contains(word)));

            return output;
        }

        public static string CleanString(string inputString, bool removeSpecialChars = true, bool removeNumbers = true, bool removeSingleChars = true, bool removeStopWords = true)
        {
            if (string.IsNullOrEmpty(inputString))
                return inputString;

            inputString = HttpUtility.HtmlDecode(inputString);
            inputString = HttpUtility.UrlDecode(inputString);

            if (!string.IsNullOrEmpty(inputString) && removeSpecialChars)
            {
                inputString = RemoveAllSpecialCharacterForIndexing(inputString);
            }

            if (!string.IsNullOrEmpty(inputString) && removeSingleChars)
            {
                inputString = RemoveSingleCharacterWordsFromString(inputString);
            }

            if (!string.IsNullOrEmpty(inputString) && removeStopWords)
            {
                inputString = RemoveStopWordsFromString(inputString);
            }


            //this method is not opted for the user. this must be removed to reduce the burden on index size.
            if (!string.IsNullOrEmpty(inputString))
            {
                inputString = RemoveLengthyWordsFromString(inputString);
            }

            if (!string.IsNullOrEmpty(inputString) && removeNumbers)
            {
                inputString = RemoveNumbersFromString(inputString);
            }


            return inputString;
        }

        static List<string> stopWords;
        public static List<string> GetStopWords()
        {
            if (stopWords == null || stopWords.Count == 0)
            {
                stopWords = new List<string> { "a", "is", "was", "have", "not", "do", "does", "come", "http", "www", "com", "html", "news", "in", "article" };

                string basePath = NewsMine.Configuration.ConfigurationManager.StorageBaseFolder;

                basePath = Path.Combine(basePath, "KeyWords");

                string stopWordsFilePath = Path.Combine(basePath, "stopwords.txt");

                if (File.Exists(stopWordsFilePath))
                {
                    System.IO.StreamReader file = new System.IO.StreamReader(stopWordsFilePath);
                    string line = string.Empty;

                    while ((line = file.ReadLine()) != null)
                    {
                        if (string.IsNullOrEmpty(line) || line.Trim() == string.Empty)
                            continue;

                        if (!stopWords.Contains(line.Trim().ToLower()))
                        {

                            stopWords.Add(line.Trim().ToLower());
                        }
                    }

                    file.Close();
                }
            }

            return stopWords;
        }


        static List<string> noiceWords;
        public static List<string> GetNoiceWords()
        {
            if (noiceWords == null || noiceWords.Count == 0)
            {
                noiceWords = new List<string>();

                string basePath = NewsMine.Configuration.ConfigurationManager.StorageBaseFolder;

                basePath = Path.Combine(basePath, "KeyWords");

                string noiceWordsFilePath = Path.Combine(basePath, "noicewords.txt");

                if (File.Exists(noiceWordsFilePath))
                {
                    System.IO.StreamReader file = new System.IO.StreamReader(noiceWordsFilePath);
                    string line = string.Empty;

                    while ((line = file.ReadLine()) != null)
                    {
                        if (string.IsNullOrEmpty(line) || line.Trim() == string.Empty)
                            continue;

                        if (!noiceWords.Contains(line.Trim().ToLower()))
                        {

                            noiceWords.Add(line.Trim().ToLower());
                        }
                    }

                    file.Close();
                }
            }

            return noiceWords;
        }
    
    }
}
